package net.borderwars.io;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

/**
 * @author Eric
 *         Date: Jul 26, 2005
 *         Time: 4:31:48 PM
 */
public class MeasuredInputStream extends FilterInputStream {
    static private Logger log = Logger.getLogger ("MeasuredInputStream");
    long createdAt;
    int totalBytes = 0;

    public MeasuredInputStream (InputStream in) {
        super (in);
        createdAt = System.currentTimeMillis ();
    }

    public long getBytesPerSecond () {
        long time = System.currentTimeMillis () - createdAt;
        if (time == 0)
            time = 1;
        return (totalBytes / time);
    }

    public int read () throws IOException {
        totalBytes++;
        return super.read ();    //To change body of overridden methods use File | Settings | File Templates.
    }

    public int read (byte b[]) throws IOException {
        int rtn = super.read (b);
        totalBytes += b.length;
        return rtn;    //To change body of overridden methods use File | Settings | File Templates.
    }

    public int read (byte b[], int off, int len) throws IOException {
        int rtn = super.read (b, off, len);
        totalBytes += len;
        return (rtn);
    }

}
